package com.jkc.xh.fragments;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.jkc.xh.R;
import com.jkc.xh.adapters.TradeSoldAllAdapter;
import com.jkc.xh.adapters.core.BaseRecyclerAdapter;
import com.jkc.xh.adapters.viewholder.HeadViewSoldAll;
import com.jkc.xh.adapters.viewholder.HeadViewSoldAll_;
import com.jkc.xh.constants.Constant;
import com.jkc.xh.fragments.core.BasePullRecyclerFragment;
import com.jkc.xh.manager.TraderManager;
import com.jkc.xh.models.HoldAllModel;
import com.jkc.xh.network.RestCallBack;
import com.jkc.xh.network.models.HoldAllResponse;
import com.jkc.xh.network.models.RestError;
import com.jkc.xh.views.widget.NavBar;
import com.jkc.xh.views.widget.pullview.PullRecyclerView;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.FragmentArg;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhangbreeze on 15/11/21.
 */
@EFragment(R.layout.fragment_sort_all)
public class TradeSoldAllFragment extends BasePullRecyclerFragment implements BaseRecyclerAdapter.OnViewClickListener {
    @ViewById(R.id.nav_bar)
    NavBar mNavBar;
    private TradeSoldAllAdapter mAdapter;
    private HeadViewSoldAll header;
    private int pageIndex = 0;
    private int pageTotal = 0;
    @FragmentArg("modelExtra")
    String extraCode;

    @Override
    protected void initView(PullRecyclerView recyclerView) {
        header = HeadViewSoldAll_.build(getContext());
        header.setHeadCallBack(headCallBack);
        mAdapter = new TradeSoldAllAdapter();
        mAdapter.setListener(this);
        mAdapter.setItemClickListener(mAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(mAdapter);
        recyclerView.setHeaderView(header);
        mNavBar.setOnMenuClickListener(new NavBar.OnMenuClickListener() {
            @Override
            public void onLeftMenuClick(View view) {
                header.closeSoftInput();
                finishFragment();
            }

            @Override
            public void onRightMenuClick(View view) {
                requestData(true);
                header.reloadStockInfo();
            }
        });
    }

    @AfterViews
    void initData() {
        if (!TextUtils.isEmpty(extraCode)) {
            header.loadStockInfo(extraCode);
        }
        requestData(true);
    }


    @Override
    protected void onRefresh(RecyclerView recyclerView) {
        requestData(true);
    }

    @Override
    protected void onLoadMore(RecyclerView recyclerView) {
        requestData(false);
    }

    private void requestData(final boolean isRefresh) {
        if (isRefresh) {
            pageIndex = 0;
            getPullRecyclerView().autoRefresh();
        } else {
            pageIndex++;
            if (pageIndex >= pageTotal) {
                return;
            }
        }
        addJob(TraderManager.queryHoldAllList(pageIndex,1, new RestCallBack<HoldAllResponse>() {
            @Override
            public void success(HoldAllResponse response) {
                pageTotal = response.pages;
                if (response.list == null || response.list.size() == 0) {
                    finishLoad(false);
                    return;
                }
                boolean isALl = false;
                if (response.list.size() < Constant.PAGE_SIZE) {
                    isALl = false;
                } else {
                    isALl = true;
                }

                List<HoldAllModel> tmp = new ArrayList<HoldAllModel>();
                for (HoldAllModel model : response.list) {
                    if (model.sell_flag == 1) {
                        tmp.add(model);
                    }
                }
                if (isRefresh) {
                    mAdapter.replaceWith(tmp);
                    mAdapter.clearCurrentMoreView();
                    if (tmp != null && tmp.size() > 0 && TextUtils.isEmpty(extraCode)) {
                        header.loadStockInfo(tmp.get(0).stock_code);
                    }
                } else {
                    mAdapter.addAll(tmp);
                }
                finishLoad(isALl);
            }

            @Override
            public void failure(RestError error) {

            }
        }));
    }

    @Override
    public void onDetach() {
        header.clearAllJob();
        super.onDetach();
    }

    @Override
    public void onItemClick(View view, Object object, int position) {
        HoldAllModel model = (HoldAllModel) object;
        switch (view.getId()) {
            case R.id.btnSoldAll:
                header.loadStockInfo(model.stock_code);
                getRecyclerView().smoothScrollToPosition(0);
                break;
            case R.id.btnSoldPart:
                TradeSoldPartFragment_.builder().build().startFragment(getContext());
                break;
        }
    }

    HeadViewSoldAll.HeadCallBack headCallBack = new HeadViewSoldAll.HeadCallBack() {

        @Override
        public void loadStart() {
            showProgress("开始加载");
        }

        @Override
        public void loadEnd() {
            hideProgress();
        }
    };
}
